package com.sc.nft.helper;

import lombok.RequiredArgsConstructor;
import org.springframework.data.util.Pair;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;


/**
 * JDBC 通常查询工具类
 */
@Component
@RequiredArgsConstructor
public class GenericQueryHelper {

    private final JdbcTemplate jdbcTemplate;

    public Long queryMaxId(@NonNull String table) {
        return jdbcTemplate.queryForObject(String.format("SELECT MAX(id) FROM %s", table), Long.class);
    }

    public Long queryMinId(@NonNull String table) {
        return jdbcTemplate.queryForObject(String.format("SELECT MIN(id) FROM %s", table), Long.class);
    }

    /**
     * 查询最小值、最大值 Pair($minId, $maxId)
     */
    public Pair<Long, Long> queryIdPair(@NonNull String table) {
        return jdbcTemplate.queryForObject(String.format("SELECT MIN(id), MAX(id) FROM %s", table),
                (rs, i) -> Pair.of(rs.getLong(1), rs.getLong(2)));
    }

}
